// 1334.7 /
#include <QtGui>
#include <QTimer>
#include <QTime>

#include <QStandardItemModel>

#include "tehanaliz.h"
#include "graph.h"
#include "plotter.h"
#include "dataContainer.h"

#include <stdlib.h>

DataContainer bigContainer;

//TODO: �������
#include <QScrollBar>
#include <QScrollArea>

TehAnaliz::TehAnaliz( QWidget *parent ) : QDialog( parent ) {
	QGridLayout *mainLayout = new QGridLayout;
	mainLayout->setColumnStretch(0,0);
	mainLayout->setColumnStretch(1,5);
	
	QGroupBox * controlsGroupBox = new QGroupBox(tr("controls"));
	QVBoxLayout * controlsLayout = new QVBoxLayout();
	controlsGroupBox->setLayout( controlsLayout);
	mainLayout->addWidget(controlsGroupBox, 0,0,1,1);
	startDateEdit = new QDateEdit();
	startDateEdit->setDisplayFormat("dd.MM.yyyy");
	startDateEdit->setDate( QDate(1996,1,1) );
	controlsLayout->addWidget(startDateEdit);
	endDateEdit = new QDateEdit();
	endDateEdit->setDisplayFormat("dd.MM.yyyy");
	endDateEdit->setDate( QDate(1996,1,1) );
	controlsLayout->addWidget(endDateEdit);
	comissionCheckbox = new QCheckBox(tr("Comission"));
	controlsLayout->addWidget(comissionCheckbox);
	comissionSlider = new QSlider();
	comissionSlider->setOrientation(Qt::Horizontal);
	controlsLayout->addWidget(comissionSlider);
	startButton = new QPushButton( tr("Start") );
	connect( startButton, SIGNAL(clicked()), this, SLOT(calcComissions()) );
	controlsLayout->addWidget(startButton);
	
	
	plot = new FirstPlot();
	mainLayout->addWidget(plot, 0, 1, 1, 1);

	/*QScrollBar *scroller = new QScrollBar( Qt::Horizontal, this );
	scroller->setValue( scroller->maximum());
	scroller->setPageStep( 40 );
	connect( scroller, SIGNAL(valueChanged(int)), plot, SLOT(scrollPlot()) );
	mainLayout->addWidget(scroller, 1, 0, 2, 1);*/
	
	QTabWidget * tablesTabWidget = new QTabWidget();
	mainLayout->addWidget( tablesTabWidget, 2, 0, 1, 2 );
	QWidget * tab1 = new QWidget(); QVBoxLayout *layout1 = new QVBoxLayout(); tab1->setLayout(layout1);
	QWidget * tab2 = new QWidget(); QVBoxLayout *layout2 = new QVBoxLayout(); tab2->setLayout(layout2);
	QWidget * tab3 = new QWidget(); QVBoxLayout *layout3 = new QVBoxLayout(); tab3->setLayout(layout3);
	QWidget * tab4 = new QWidget(); QVBoxLayout *layout4 = new QVBoxLayout(); tab4->setLayout(layout4);

	bigContainer.readFromCSV();

	QStandardItemModel * avgModel = new QStandardItemModel(1, 1);
	QStandardItemModel * minModel = new QStandardItemModel(1, 1);
	avgModel->setHorizontalHeaderLabels(bigContainer.nameIndex);
	avgModel->setVerticalHeaderLabels(bigContainer.dateIndex);
	minModel->setHorizontalHeaderLabels(bigContainer.nameIndex);
	minModel->setVerticalHeaderLabels(bigContainer.dateIndex);
	//bigContainer.fillAvgModel(avgModel);
	//bigContainer.fillMinModel(minModel);

	QTableView *avgPriceTable = new QTableView();
	QTableView *minPriceTable = new QTableView();
	avgPriceTable->setModel(avgModel);
	minPriceTable->setModel(minModel);
	layout1->addWidget( avgPriceTable );
	layout2->addWidget( minPriceTable );
	tablesTabWidget->addTab(tab1, tr("avg"));
	tablesTabWidget->addTab(tab2, tr("min"));
	
	tablesTabWidget->addTab(tab3, tr("max"));
	
	tablesTabWidget->addTab(tab4, tr("last"));
	
	connect( this, SIGNAL(fitTableWidthHeight(void)), avgPriceTable, SLOT(resizeColumnsToContents(void)) );
	connect( this, SIGNAL(fitTableWidthHeight(void)), avgPriceTable, SLOT(resizeRowsToContents(void)) );
	connect( this, SIGNAL(fitTableWidthHeight(void)), minPriceTable, SLOT(resizeColumnsToContents(void)) );
	connect( this, SIGNAL(fitTableWidthHeight(void)), minPriceTable, SLOT(resizeRowsToContents(void)) );
	emit( fitTableWidthHeight() );
	
	setLayout(mainLayout);  
	setWindowTitle(tr("TehAnaliz v0.4"));

	resize(1000, 800);
}

void TehAnaliz::calcComissions() {
	Graph * newGraph = new Graph();    //TODO:  delete Graph after use!
	bigContainer.priceToGraph("24004", newGraph);
	newGraph->updateData();
	newGraph->curve->setColor(Qt::red);
	plot->attachGraph( newGraph );
	
	newGraph = new Graph();
	bigContainer.priceToGraph("24005", newGraph);
	newGraph->updateData();
	plot->attachGraph(newGraph);
	
	newGraph = new Graph();
	bigContainer.comissionsToGraph(startDateEdit->date(), newGraph);
	newGraph->updateData();
	plot->attachGraph( newGraph );
	 
	plot->replot();
}
